use std::ffi::{CStr, CString};
use std::path::{Path, PathBuf};
use std::os::raw::c_char;

#[link(name = "preprocess")]
extern "C" {
    fn expand_subckt_c(origin_file_path: *const c_char) -> *const c_char;
}

pub fn expand_subckt<P: AsRef<Path>>(origin_file_path: P) -> Option<PathBuf> {
    let c_path = CString::new(
        origin_file_path.as_ref().to_str().expect("Path contains invalid UTF-8"),
    ).expect("CString::new failed");

    unsafe {
        let c_result = expand_subckt_c(c_path.as_ptr());
        if c_result.is_null() {
            None
        } else {
            let result = CStr::from_ptr(c_result).to_str().expect("Invalid UTF-8");
            Some(PathBuf::from(result))
        }
    }
}

